package org.clockworkmages.games.anno1186.util;

/**
 * Utility class for calculating chances of rolls based on the value of a Skill
 * or Stat.
 */
public class RollUtil {
	/**
	 * Chance in a 1-100 (%) range, with power=0 resulting in chance=0. The
	 * greater the power, the greater the chance, but the chance can never reach
	 * 100.
	 * 
	 * @param power
	 *            power
	 * @return
	 */
	public static int chance(double power) {
		double chance = (1 - Math.pow(0.98, power)) * 100;
		return (int) chance;
	}

	/**
	 * Chance in a 1-100 (%) range, with power=0 resulting in chance=50%.
	 * power>0 mans chance increasing (over 50%) and pow<0 means chance
	 * decreasing (below 50%). The greater the power, the greater the chance,
	 * but the chance can never reach 100.
	 * 
	 * @param power
	 *            power
	 * @return
	 */
	public static int chanceBase50(double power) {
		int sign = power < 0 ? -1 : 1;
		double chance = 50 + sign * 50 * (1 - Math.pow(0.99, Math.abs(power)));
		return (int) chance;
	}

	/**
	 * Roll with a in a 1-100 (%) range, with pow=0 resulting in chance of a
	 * <b>true</b> result being equal to 0.
	 * 
	 * @param power
	 *            power
	 * @return
	 */
	public static boolean roll(double power) {
		double roll = random(100);
		int chance = chance(power);
		return roll <= chance;
	}

	public static boolean rollBase50(double power) {
		double roll = random(100);
		int chance = chanceBase50(power);
		return roll <= chance;
	}

	public static double random(int range) {
		return Math.random() * range;
	}
}
